package com.mycompany.j2eem.controller;

import com.mycompany.j2eem.Aop.JudgeLog;
import com.mycompany.j2eem.Entity.JsonResult;
import com.mycompany.j2eem.Entity.ResultCode;
import com.mycompany.j2eem.service.wucyservice.PersonHubService;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
public class PersonalHubController {

    @Autowired
    PersonHubService personHubService;

    private String getUsername(HttpSession session) {
        return session.getAttribute("username").toString();
    }

    //单张图片修改是否公开
    @RequestMapping("/ispublic")
    public JsonResult doIspublic(@RequestParam("pictureid") String pictureid) {
        System.out.println(pictureid);
        personHubService.changeState(pictureid);
        return new JsonResult(ResultCode.SUCCESS);
    }

    @RequestMapping("/ispubliclist")
    public JsonResult doIspublicList(@RequestParam("pictureid") List<String> pictureid) {
        System.out.println(pictureid);
        personHubService.changeState(pictureid);
        return new JsonResult(ResultCode.SUCCESS);
    }

    //收藏到收藏夹
    @RequestMapping("/favorite")
    public JsonResult doFavorite(@RequestParam("favoriteID") String favoriteId, @RequestParam("pictureId") String pictureId) {
        personHubService.favoritePicture(pictureId, favoriteId);
        return new JsonResult(ResultCode.SUCCESS);
    }

    //获取收藏夹分类名字列表
    @RequestMapping("/getFavoriteName")
    public JsonResult doGetFavoriteName(HttpSession session) {
        String username = getUsername(session);
        return personHubService.getFavoriteList(username);
    }

    //创建收藏夹分类
    @RequestMapping("/createFavorite")
    public JsonResult doCreateFavorite(HttpSession session, @RequestParam("favoriteName") String favoriteName) {
        String username = getUsername(session);
        return personHubService.createFavorite(favoriteName, username);
    }

    //移动到其他收藏夹
    @RequestMapping("/moveFavorite")
    public JsonResult doMoveFavorite(@RequestParam("newFavoriteId") String newFavoriteId, @RequestParam("pictureId") String pictureId, HttpSession session) {
        String username = getUsername(session);
        return personHubService.moveFavorite(newFavoriteId, pictureId, username);
    }

    //删除收藏夹分类
    @RequestMapping("/deleteFavorite")
    public JsonResult doDeleteFavorite(@RequestParam("favoriteId") String favoriteId) {
        return personHubService.deleteFavorite(favoriteId);
    }

    //取消收藏
    @RequestMapping("/cancelFavorite")
    public JsonResult doCancelFavorite(@RequestParam("FavoriteId") String FavoriteId, @RequestParam("pictureId") String pictureId) {
        return personHubService.cancelFavorite(pictureId, FavoriteId);
    }

    //获取个人仓库名字列表
    @RequestMapping("/getPersonalHubName")
    public JsonResult doGetPersonalHubName(HttpSession session) {
        String username = getUsername(session);
        return personHubService.getPersonalHubList(username);
    }

    //个人仓库创建分类
    @RequestMapping("/createPersonalHub")
    public JsonResult doCreatePersonalHub(@RequestParam("PersonalHubName") String PersonalHubName, HttpSession session) {
        String username = getUsername(session);
        return personHubService.createPersonalHub(PersonalHubName, username);
    }

    //移动到其他类型的仓库
    @RequestMapping("/moveHub")
    public JsonResult doMoveHub(@RequestParam("PersonalHubId") String newPersonalHubId, @RequestParam("pictureId") String pictureId, HttpSession session) {
        String username = getUsername(session);
        return personHubService.movePersonlHub(newPersonalHubId, pictureId, username);
    }

    //批量移动到其他仓库
    @RequestMapping("/moveListHub")
    public JsonResult doMoveHub(@RequestParam("PersonalHubId") String newPersonalHubId, @RequestParam("pictureId") List<String> pictureId, HttpSession session) {
        String username = getUsername(session);
        pictureId.stream().forEach(x -> {
            personHubService.movePersonlHub(newPersonalHubId, x, username);
        });
        return new JsonResult(ResultCode.SUCCESS);
    }

    //删除图片
    @RequestMapping("/deleteOneImage")
    public JsonResult doDeleteOneImage(@RequestParam("pictureId") String pictureId, @RequestParam("PersonalHubId") String PersonalHubId) {
        return personHubService.deletePersonalPicture(pictureId, PersonalHubId);
    }

    @RequestMapping("/deleteImages")
    public JsonResult doDeleteImages(@RequestParam("pictureId") List<String> pictureId, @RequestParam("PersonalHubId") String PersonalHubId) {
        return personHubService.deletePersonalPicture(pictureId, PersonalHubId);
    }

    //删除某个个人仓库
    @RequestMapping("/deletePersonlHub")
    public JsonResult doDeletePersonalHub(@RequestParam("PersonalHubId") String PersonalHubId) {
        return personHubService.deletePersonalHub(PersonalHubId);
    }

    //上传到个人仓库
    @RequestMapping("/upLoadToPersonal")
    public JsonResult doUpLoadToPersonal(@RequestParam("PersonalHubId") String PersonalHubId, @RequestParam("Images") MultipartFile file) {
        return personHubService.upLoad(file, PersonalHubId);
    }

    //发布
    @RequestMapping("/fabu")
    public JsonResult doFabu(@RequestParam("ImagesId") List<String> ImahesId, @RequestParam("TagName") List<String> TagName, @RequestParam("title") String title, HttpSession session, @RequestParam("available") String available) {
        String username = getUsername(session);
        return personHubService.fabu(ImahesId, TagName, username, title, available);
    }

    //Tag如果没有需要添加
    @RequestMapping("/addTag")
    public JsonResult doAddTag(@RequestParam("TagName") List<String> TagName) {

        return personHubService.addTag(TagName);
    }

    //获取仓库图片
    @RequestMapping("/getHubPicture")
    public JsonResult getHubPicture(@RequestParam("HubId") String hubId, HttpSession session) {
        String username = getUsername(session);

        return personHubService.getHubPicture(hubId, username);
    }

    //获取收藏夹图片
    @JudgeLog
    @RequestMapping("/favoritePicture")
    public JsonResult getfavoritePicture(@RequestParam("HubId") String hubId) {
        return personHubService.getFavoritePicture(hubId);
    }

    //喜欢
    @RequestMapping("/likepicture")
    public JsonResult likePicure(@RequestParam("PictureId") String pictureId, HttpSession session) {
        String username = getUsername(session);
        return personHubService.likePicture(username, pictureId);
    }

    //获取喜欢列表
    @RequestMapping("/likeList")
    public JsonResult doLikeList(HttpSession session) {
        String username = getUsername(session);
        return personHubService.likeList(username);
    }

    //获取自己动态
    @RequestMapping("/getMovement")
    public JsonResult doGetMovement(HttpSession session) {
        String username = getUsername(session);
        return personHubService.getMovementInformation(username);
    }

    @RequestMapping("/getOtherMovement")
    public JsonResult doGetPtherMovement(@RequestParam("userId") String userId) {
        return personHubService.getOtherMovementInformation(userId);
    }

    //获取关注人的动态
    @JudgeLog
    @RequestMapping("/getFollowMovement")
    public JsonResult doGetFollowMovement(HttpSession session) {
        String username = getUsername(session);
        return personHubService.getFollowMovement(username);
    }

    //是否收藏
    @RequestMapping("/isFavorite")
    public JsonResult doIsFavorite(@RequestParam("PictureId") String pictureId, HttpSession session) {
        String username = getUsername(session);
        return personHubService.isFavorite(username, pictureId);
    }

    //返回图片信息
    @JudgeLog
    @RequestMapping("/ImageInfor")
    public JsonResult doImageInfor(@RequestParam("PictureId") String pictureId, HttpSession session) {
        String username = getUsername(session);
        Object admin = session.getAttribute("username");
        if (admin == null) {
            return personHubService.getNoLoginPictureInfor(pictureId);
        }
        return personHubService.getPictureInfor(pictureId, username);
    }
}
